package com.shop2cn.iapi.openapibiz.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.openapibiz.parameter.AuthorizeBean;
import com.shop2cn.iapi.openapibiz.parameter.ChangeAuthCodeBean;
import com.shop2cn.iapi.openapibiz.parameter.NewThirdPartyAppBean;
import com.shop2cn.iapi.openapibiz.service.ChangeAuthCodeCall;
import com.shop2cn.iapi.openapibiz.service.NewThirdPartyAppCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.OpenApiBizCallService;
import com.ymttest.database.model.Application;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;
/**
*取消授权app
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ChangeAuthCode{
	private static ChangeAuthCodeBean changeauthcodeBean;
	private static ChangeAuthCodeCall changeauthcodeCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("取消授权app");
	}

	@Before
	public void caseUp() {
		changeauthcodeBean=new ChangeAuthCodeBean();
		changeauthcodeCall=new ChangeAuthCodeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(changeauthcodeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeAuthCode_001() {
		Logger.start(true,"更换授权码-自主开发");
		try {
			
			OpenApiBizCallService OpenApiBizCallService = new OpenApiBizCallService();
			
			AuthorizeBean authorizeBean = OpenApiBizCallService.createAuthorizeBean_USER_DEV();
			
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper.selectApplicationByAppName(authorizeBean.getAppName()).get(0);
			AuthCode authCode =  openApiWapper.selectAuthCodeByAppId(application.getApp_id()).get(0);		
		
			changeauthcodeBean.setSellerId(authorizeBean.getSellerId());
			changeauthcodeBean.setAuthCode(authCode.getAuth_code());
			changeauthcodeBean.setAppId(application.getApp_id());
		
			changeauthcodeCall.setData(changeauthcodeBean);
			changeauthcodeCall.callService();
			
			Logger.verifyEquals(ECode.SUCCESS, changeauthcodeCall.getCode(), "验证Code");
			Logger.verifyEquals("更换授权成功!", changeauthcodeCall.getMessage(), "验证Message");

			OpenApiWapper openApiWapper1 = new OpenApiWapper();
			Application application1 = openApiWapper1.selectApplicationById(changeauthcodeBean.getAppId()).get(0);
			AuthCode authCode1 =  openApiWapper1.selectAuthCodeByAppId(application1.getApp_id()).get(0);
			
			Logger.verifyEquals("USER_DEV", application1.getType(), "验证数据库type");
			Logger.verifyEquals("ENABLE", application1.getStatus(), "验证数据库Status");
			Logger.verifyEquals(authorizeBean.getAppDesc(), application1.getApp_desc(), "验证数据库App_desc一致");
				
			Logger.verifyEquals(false, authCode1.getAuth_code().equals(changeauthcodeBean.getAuthCode()), "验证数据库Auth_Code改变");
			
			int day = (int) ((authCode1.getExpire_time().getTime()-authCode1.getCreate_time().getTime())/(1000*60*60*24));
			Logger.verifyEquals(true,day<366, "验证数据库ExpireTime");
			Logger.verifyEquals(true,day>363, "验证数据库ExpireTime");
			
			AuthCode code = new OpenApiWapper().selectAuthCodeById(authCode.getId()).get(0);
			Logger.verifyEquals("DISABLE", code.getStatus(), "验证数据库Status");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeAuthCode_002() {
		Logger.start(true,"更换授权码-第三方应用");
		try {

			OpenApiBizCallService OpenApiBizCallService = new OpenApiBizCallService();
			
			NewThirdPartyAppBean newThirdPartyAppBean = OpenApiBizCallService.createNewThirdPartyAppBean();
			NewThirdPartyAppCall newThirdPartyAppCall = OpenApiBizCallService.newthirdpartyappCall(newThirdPartyAppBean);
	
			AuthorizeBean authorizeBean = OpenApiBizCallService.createAuthorizeBean_THIRD_PARTY(newThirdPartyAppCall.getAppId());
			
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper.selectApplicationById(authorizeBean.getAppId()).get(0);
			AuthCode authCode =  openApiWapper.selectAuthCodeByAppId(application.getApp_id()).get(0);		
		
			changeauthcodeBean.setSellerId(authorizeBean.getSellerId());
			changeauthcodeBean.setAuthCode(authCode.getAuth_code());
			changeauthcodeBean.setAppId(application.getApp_id());
		
			changeauthcodeCall.setData(changeauthcodeBean);
			changeauthcodeCall.callService();
			
			Logger.verifyEquals(ECode.SUCCESS, changeauthcodeCall.getCode(), "验证Code");
			Logger.verifyEquals("更换授权成功!", changeauthcodeCall.getMessage(), "验证Message");

			OpenApiWapper openApiWapper1 = new OpenApiWapper();
			Application application1 = openApiWapper1.selectApplicationById(changeauthcodeBean.getAppId()).get(0);
			AuthCode authCode1 =  openApiWapper1.selectAuthCodeByAppId(application1.getApp_id()).get(0);
			
			Logger.verifyEquals("THIRD_PARTY", application1.getType(), "验证数据库type");
			Logger.verifyEquals("ENABLE", application1.getStatus(), "验证数据库Status");
			// 第三方应用没有描述
//			Logger.verifyEquals(authorizeBean.getAppDesc(), application1.getApp_desc(), "验证数据库App_desc一致");
				
			Logger.verifyEquals(false, authCode1.getAuth_code().equals(changeauthcodeBean.getAuthCode()), "验证数据库Auth_Code改变");
			
			int day = (int) ((authCode1.getExpire_time().getTime()-authCode1.getCreate_time().getTime())/(1000*60*60*24));
			Logger.verifyEquals(true,day<366, "验证数据库ExpireTime");
			Logger.verifyEquals(true,day>363, "验证数据库ExpireTime");
			
			AuthCode code = new OpenApiWapper().selectAuthCodeById(authCode.getId()).get(0);
			Logger.verifyEquals("DISABLE", code.getStatus(), "验证数据库Status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeAuthCode_003() {
		Logger.start(true,"sellerId不传");
		try {
			changeauthcodeBean.setAuthCode(TestDataManager.getRandomNum(10));
			changeauthcodeBean.setAppId(TestDataManager.getRandomNum(10));
		
			changeauthcodeCall.setData(changeauthcodeBean);
			changeauthcodeCall.callService();	
			
			Logger.verifyEquals("400", changeauthcodeCall.getCode(), "验证Code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeAuthCode_004() {
		Logger.start(true,"sellerId传0");
		try {
			changeauthcodeBean.setSellerId(0);
			changeauthcodeBean.setAuthCode(TestDataManager.getRandomNum(10));
			changeauthcodeBean.setAppId(TestDataManager.getRandomNum(10));
		
			changeauthcodeCall.setData(changeauthcodeBean);
			changeauthcodeCall.callService();			
			
			Logger.verifyEquals("400", changeauthcodeCall.getCode(), "验证Code");
			Logger.verifyEquals("不存在此授权信息，更换授权码失败!", changeauthcodeCall.getMessage(), "验证Message");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeAuthCode_005() {
		Logger.start(true,"appId传空");
		try {
			changeauthcodeBean.setSellerId(TestDataManager.getRandomIntNum(10));
			changeauthcodeBean.setAuthCode(TestDataManager.getRandomNum(10));
		
			changeauthcodeCall.setData(changeauthcodeBean);
			changeauthcodeCall.callService();		
			
			Logger.verifyEquals("400", changeauthcodeCall.getCode(), "验证Code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeAuthCode_006() {
		Logger.start(true,"appId不存在");
		try {
			
			OpenApiBizCallService OpenApiBizCallService = new OpenApiBizCallService();
			
			AuthorizeBean authorizeBean = OpenApiBizCallService.createAuthorizeBean_USER_DEV();
//			AuthorizeCall authorizeCall = OpenApiBizCallService.authorizeCall(authorizeBean);
			
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper.selectApplicationByAppName(authorizeBean.getAppName()).get(0);
			AuthCode authCode =  openApiWapper.selectAuthCodeByAppId(application.getApp_id()).get(0);		
		
			changeauthcodeBean.setSellerId(authorizeBean.getSellerId());
			changeauthcodeBean.setAuthCode(authCode.getAuth_code());
			changeauthcodeBean.setAppId(application.getApp_id()+"ss");
	
		
			changeauthcodeCall.setData(changeauthcodeBean);
			changeauthcodeCall.callService();			
			
			Logger.verifyEquals("400", changeauthcodeCall.getCode(), "验证Code");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeAuthCode_007() {
		Logger.start(true,"authCode不传");
		try {
			changeauthcodeBean.setSellerId(TestDataManager.getRandomIntNum(10));
			changeauthcodeBean.setAuthCode(TestDataManager.getRandomNum(10));
			changeauthcodeBean.setAppId(TestDataManager.getRandomNum(10));
		
			changeauthcodeCall.setData(changeauthcodeBean);
			changeauthcodeCall.callService();	
			
			Logger.verifyEquals("400", changeauthcodeCall.getCode(), "验证Code");
			Logger.verifyEquals("不存在此授权信息，更换授权码失败!", changeauthcodeCall.getMessage(), "验证Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeAuthCode_008() {
		Logger.start(true,"authCode不正确");
		try {

			OpenApiBizCallService OpenApiBizCallService = new OpenApiBizCallService();
			
			AuthorizeBean authorizeBean = OpenApiBizCallService.createAuthorizeBean_USER_DEV();
//			AuthorizeCall authorizeCall = OpenApiBizCallService.authorizeCall(authorizeBean);
			
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper.selectApplicationByAppName(authorizeBean.getAppName()).get(0);
			AuthCode authCode =  openApiWapper.selectAuthCodeByAppId(application.getApp_id()).get(0);		
		
			changeauthcodeBean.setSellerId(authorizeBean.getSellerId());
			changeauthcodeBean.setAuthCode(authCode.getAuth_code()+"dd");
			changeauthcodeBean.setAppId(application.getApp_id());
	
			changeauthcodeCall.setData(changeauthcodeBean);
			changeauthcodeCall.callService();			
			
			Logger.verifyEquals("400", changeauthcodeCall.getCode(), "验证Code");
			Logger.verifyEquals("不存在此授权信息，更换授权码失败!", changeauthcodeCall.getMessage(), "验证Message");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}